package com.xxd.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xxd.gulimail.product.entity.ProductAttrValueEntity;
import com.xxd.gulimail.product.service.ProductAttrValueService;
import com.xxd.gulimail.product.vo.AttrVo;
import com.xxd.gulimail.product.vo.AttrespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xxd.gulimail.product.entity.AttrEntity;
import com.xxd.gulimail.product.service.AttrService;
import com.xxd.common.utils.PageUtils;
import com.xxd.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author xxd
 * @email 2132862687@qq.com
 * @date 2022-09-26 10:32:57
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;
//  /product/attr/update/13
@PostMapping("/update/{spuId}")
public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities) {
     productAttrValueService.updateSpuAttr(spuId,entities);
    return R.ok();
}
//    product/attr/base/listforspu/11
    /**
     * @Description: 查询spu商品对应的属性分组和属性；
     * @Author: xuxd
     * @Date: 2022/10/19 16:49
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseListforspu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> data = productAttrValueService.baseListforspu(spuId);
        return R.ok().put("data", data);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId ,@PathVariable("attrType")String attrType) {
        PageUtils page = attrService.findPage(params,catelogId,attrType);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        //AttrEntity attr = attrService.getById(attrId);
        AttrespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
